package cn.iocoder.yudao.module.infra.controller.app.config;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.controller.app.config.vo.AppConfigRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "用户 App - 配置文件")
@RestController
@RequestMapping("/infra/config")
@Validated
@Slf4j
public class AppConfigController {

    @Resource
    private ConfigService configService;

    @GetMapping("/list")
    @Operation(summary = "获取所有公开的键值对列表")
    public CommonResult<List<AppConfigRespVO>> uploadFile() throws Exception {
        final List<ConfigDO> allPublicConfigs = configService.getAllPublicConfigs();
        final List<AppConfigRespVO> vos = allPublicConfigs.stream().map(v -> {
            final AppConfigRespVO vo = BeanUtils.toBean(v, AppConfigRespVO.class);
            vo.setKey(v.getConfigKey());
            return vo;
        }).collect(Collectors.toList());
        return CommonResult.success(vos);
    }

}
